package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.response.ResponseResult;
import com.woniuxy.system.demol.entity.SystemDepot;
import com.woniuxy.system.demol.entity.SystemSector;
import com.woniuxy.system.demol.entity.User;
import com.woniuxy.system.service.SystemDepotService;
import com.woniuxy.system.service.SystemSectorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 仓库详情表; 前端控制器
 * </p>
 *
 * @author why
 * @since 2023-09-08
 */
@RestController
@RequestMapping("/systemDepot")
public class SystemDepotController {
    @Resource
    private SystemDepotService systemDepotService;
    @GetMapping("/findall/{currentPage}/{pageSize}")
    public ResponseResult<Page<SystemDepot>> findall(@PathVariable("currentPage")Integer currentPage, @PathVariable("pageSize")Integer pageSize ){
        Page<SystemDepot> page = new Page<>(currentPage, pageSize);
        Page<SystemDepot> page1 = systemDepotService.page(page, null);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",page1);
    }

    @GetMapping("/findallExport/{currentPage}/{pageSize}")
    public ResponseResult<Page<SystemDepot>> findallExport(@PathVariable("currentPage")Integer currentPage, @PathVariable("pageSize")Integer pageSize ){
        Page<SystemDepot> page = new Page<>(currentPage, pageSize);
        Page<SystemDepot> page1 = systemDepotService.page(page, null);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",page1);
    }
    @PreAuthorize("hasAnyAuthority('depot:agreeIn','depot:agreeOut')")
    @PostMapping("/addSector")
    public ResponseResult<Boolean> addSector(@RequestBody SystemDepot systemDepot){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = formatter.format(date);
        SystemDepot systemDepot1 = new SystemDepot().setState(0).setAccount(systemDepot.getAccount()).setTime(format)
                .setDescription(systemDepot.getDescription())
                .setSize(systemDepot.getSize());
        //增加用户信息
        Boolean save = systemDepotService.save(systemDepot1);
        if (save){
                return new ResponseResult<>(ResponseResult.SUCCESS,"success",save);
        }
        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
    }
    @GetMapping("/findStateOpen/{currentPage}/{pageSize}")
    public ResponseResult<Page<SystemDepot>> findStateOpen(@PathVariable("currentPage")Integer currentPage,@PathVariable("pageSize")Integer pageSize ){
        Page<SystemDepot> page = new Page<>(currentPage, pageSize);
        QueryWrapper<SystemDepot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 1);

        Page<SystemDepot> page1 = systemDepotService.page(page, queryWrapper);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",page1);
    }

    //查看所有禁用的
    @GetMapping("/findStateClose/{currentPage}/{pageSize}")
    public ResponseResult<Page<SystemDepot>> findStateClose(@PathVariable("currentPage")Integer currentPage,@PathVariable("pageSize")Integer pageSize ){
        Page<SystemDepot> page = new Page<>(currentPage, pageSize);
        QueryWrapper<SystemDepot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 3);

        Page<SystemDepot> page1 = systemDepotService.page(page, queryWrapper);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",page1);
    }
    //搜索所有
    @GetMapping("/searchAll/{num}/{input}/{currentPage}/{pageSize}")
    public ResponseResult<Page<SystemDepot>> findStateClose(@PathVariable("num")Integer num,@PathVariable("input")String input,@PathVariable("currentPage")Integer currentPage,@PathVariable("pageSize")Integer pageSize ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("uid", 1001);
        Page<SystemDepot> all2 = systemDepotService.searchAll(num,input,currentPage, pageSize);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",all2);
    }
    @PreAuthorize("hasAnyAuthority('depot:agreeIn','depot:agreeOut')")
    @PostMapping("/updateSector")
    public ResponseResult<Boolean> updateSector(@RequestBody SystemDepot systemDepot){

        SystemDepot systemDepot1 = new SystemDepot().setId(systemDepot.getId());


        if (systemDepot.getAccount()!=""||systemDepot.getAccount()!=null|| !Objects.isNull(systemDepot.getAccount())){
            systemDepot1.setAccount(systemDepot.getAccount());
        }else if (systemDepot.getDescription()!=""||systemDepot.getDescription()!=null|| !Objects.isNull(systemDepot.getDescription())){
            systemDepot1.setDescription(systemDepot.getDescription());
        }else if (systemDepot.getSize()!=0 || !Objects.isNull(systemDepot.getSize())){
            systemDepot1.setSize(systemDepot.getSize());
        }
        //增加用户信息
        Boolean save = systemDepotService.saveOrUpdate(systemDepot1);
        if (save){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",save);
        }
        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
    }


}

